import requests
import pymongo
import time

client = pymongo.MongoClient()
db = client.douban
col_movie = db.movies
col_casts = db.casts

def get_cast(id):
    if not id:
        return
    print('fetching', id)
    try:
        url = 'https://api.douban.com/v2/movie/celebrity/' + str(id)
        print(url)
        req = requests.get(url)
        data = req.json()
        print('inserting', data['name'])
        col_casts.update_one({'id': data['id']}, {'$set': data}, upsert=True)
        print('finish', data['name'])
    except Exception as e:
        print(e, id)

for movie in col_movie.find():
    casts = movie['casts']
    print(movie['title'])
    for cast in casts:
        print(cast['name'], cast['id'])
        get_cast(cast['id'])
        time.sleep(1.5)
    print('======================')
